package com.cnctema.easecalendar_weekly_view.database

import androidx.lifecycle.LiveData
import androidx.room.*
import com.cnctema.easecalendar_weekly_view.database.Event

/**
 * @ClassName: EventDatabaseDao
 * @Description: 日历事件数据库映射(类的作用描述)
 * @Author: cnctema
 * @CreateDate: 2020/5/26 3:06
 */
@Dao
interface EventDatabaseDao {
    @Insert
    fun insert(event: Event)
    @Update
    fun update(event: Event)
    @Delete
    fun delete(event: Event)

    @Query("DELETE from usual_event_table WHERE eventId = :key")
    fun deleteById(key: Long)

    @Query("SELECT * from usual_event_table WHERE eventId = :key")
    fun get(key:Long): Event?

    @Query("DELETE FROM usual_event_table")
    fun clear()

    @Query("DELETE FROM usual_event_table WHERE event_type =:type")
    fun clearEventByType(type:Int)

    @Query("DELETE FROM usual_event_table WHERE event_type =:type and event_lastsync < :syncTime")
    fun clearCourseEvent(type:Int,syncTime:Long)

    @Query("SELECT * FROM usual_event_table ORDER BY eventId DESC LIMIT 1")
    fun getEvent(): Event?

    @Query("SELECT * FROM usual_event_table WHERE start_time_milli<:edMilli and end_time_milli>:stMilli ORDER BY eventId DESC")
    fun getEventsByTimeRange(stMilli:Long,edMilli:Long): List<Event>

    @Query("SELECT * FROM usual_event_table ORDER BY start_time_milli DESC")
    fun getAllEventsToLiveData(): LiveData<List<Event>>

}
